<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use think\config;
use Qiniu\Auth as Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;

class Picture extends Islogin
{

    // 加载全部的图片
    public function Index()
    {
        $session = Session::get('hmy_user');

        $id = $session['id'];

        $category = DB::name('category')->where('pid','0,')->paginate(4);

        $list = Db::table(['picture' => 'p','category'=>'c'])
                    ->field('p.id,p.status,p.title,p.src,c.name')
                    ->where('c.id = p.cate_id && user_id ='.$id)
                    ->order('p.id','desc')
                    ->select();
        // var_dump($list);

        $list_num = count($list);

        // var_dump($list);
        return view('index@picture/index',[
            'session' => $session,
            'category' => $category,
            'list' => $list,
            'list_num' => $list_num
        ]);

    }

     // 上传图片
    public function upload()
    {

        // 用户id
        $session = Session::get();

        $id = $session['hmy_user']['id'];


        // var_dump($_POST);
        //接收文件
        $file = request()->file('file');
        // var_dump($file);die;

        $file = empty($file)?$this->error('请上传您的文件'): $file;

        // 要上传图片的本地路径
        $filePath = $file->getRealPath();

        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀


        $img = ['png', 'jpg', 'jpeg'];

        if (!in_array($ext, $img)){

            return $this->error('上传的文件必须为图片格式!');

        }


        // 上传到七牛后保存的文件名
        $key = substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写 Access Key 和 Secret Key
        $accessKey = Config::get('qiniu.accessKey');
        $secretKey = Config::get('qiniu.secretKey');

        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);

        // 要上传的空间
        $bucket = Config::get('qiniu.bucket');
        $domain = Config::get('qiniu.DOMAIN');

        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if($err == null){
            // 这一块要变成自己数据表需要的字段
            $url ="http://phv2jzzej.bkt.clouddn.com/".$ret['key'];

            $list['src'] = $ret['key'];

            $list['cate_id'] = $_POST['cid'];

            $list['title'] = $_POST['name'];

            $list['user_id'] = $id;

            $list['upload_time'] = date('Y.m.d',time());

            Db::table('picture')->insert($list);

            return  $this->success('新增图片成功');

        } else {

            return  $this->error('新增图片失败');

        }

    }


     // 图片详情
    public function detail($id)
    {

        $session = Session::get('hmy_user');
        // var_dump($session);
        $uid = $session['id'];

        // 图片详情
        $user = DB::table(['hmy_user' =>'u','picture' =>'p'])
                    ->where('u.id = p.user_id && p.user_id = '.$uid.' && p.id = '.$id)
                    ->find();

        // var_dump($user);


        $cate = DB::table('category')->where('id',$user['cate_id'])->find();


        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);


        // 查询评论
        $comment = Db::table(['comment' =>'c','picture' =>'p','hmy_user' => 'u'])
                    ->where('c.pid = p.id && p.id = '.$id.' && u.id = p.user_id')
                    ->field('c.id,c.name,c.reply,p.upload_time,p.title,c.content,c.pid,c.uid,u.icon,p.src,c.commenttime,p.abstruct')
                    ->select();


        // 遍历分类(添加)
        $res = Db::table('category')
                    ->order('path', 'id')
                    ->select();

        // var_dump($comment);


        // 粉丝数量
        $comment_num = count($comment);


        return view('index@picture/detail',[
            'category' => $category,
            'session' => $session,
            'comment' => $comment,
            'user' => $user,
            'cate' => $cate,
            'res' => $res,
            'comment_num' => $comment_num
        ]);

    }


    // 执行修改图片信息
    public function pic_details(Request $request)
    {
        // 接收数据
        $post = $request->get();
        // var_dump($post);

        // 获取id
        $id = $request->get('id');
        // var_dump($id);

        // 查询数据
        // $data = DB::table('picture')->find($id);
        $data['cate_id'] = $post['cid'];
        $data['id'] = $post['id'];
        $data['title'] = $post['title'];
        $data['abstruct'] = $post['abstruct'];

        // 执行sql语句
        $result = Db::table('picture')->where('id', $id)->update($data);


        if ($result) {

            $info['status'] = true;
            $info['msg'] = '修改成功!';

            return json($info);

        } else {

            $info['status'] = false;
            $info['msg'] = '修改失败,请重试...';

            return json($info);;

        }
    }


    // 删除图片
    public function del_pic()
    {
        $id = input('id');

        $comment = Db::table('comment')
                    ->where('pid',$id)
                    ->delete();

        $list = Db::table('picture')
                    ->where('id',$id)
                    ->delete();

        if ($list > 0) {

            $info['status'] = true;
            $info['msg'] = '删除成功!';

            return json($info);

        } else {

            $info['status'] = false;
            $info['msg'] = '删除失败,请重试...';

            return json($info);;

        }


    }



     // 回复评论
    public function reply(Request $request)
    {

        // 接收数据
        $data = $request->get();
        // var_dump($data);die;
        // 获取ID
        $session = Session::get();


        $id = $data['id'];


        // 插入sql
        $result = Db::table('comment')->where('id',$id)->update($data);

       // 跳转

        if ($result > 0) {

            $info['status'] = true;
            $info['msg'] = '回复成功!';

            return json($info);

        } else {

            $info['status'] = false;
            $info['msg'] = '回复失败,请重试...';

            return json($info);;

        }
    }



    // 评论列表
    public function comment()
    {

        $session = Session::get();


        $id = $session['hmy_user']['id'];

        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);


        // 查询评论
        $comment = Db::table(['comment' =>'m','hmy_user' =>'u'])
                    ->where('m.uid = u.id && m.uid = '.$id)
                    ->field('m.id,m.content,m.commenttime,m.reply,u.icon,u.name,m.pid')
                    ->select();

        // var_dump($comment);

        // 粉丝数量
        $comment_num = count($comment);


        return view('index@user/comment',[
            'session' => $session,
            'category' => $category,
            'comment' => $comment,
            'comment_num' => $comment_num
        ]);

    }


}
